package com.mes.eventhandler;

import javax.enterprise.event.Observes;
import org.apache.log4j.Logger;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.model.Property;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import com.mes.quality.data.EmesqInspectionMethod;

public class EmesqInspectionMethodEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EmesqInspectionMethod.ENTITY_NAME) };
	protected Logger logger = Logger.getLogger(this.getClass());

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}
	
	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
	}
	
	public void onUpdate(@Observes EntityUpdateEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		final Entity eimEntity = ModelProvider.getInstance().getEntity(EmesqInspectionMethod.ENTITY_NAME);
		final EmesqInspectionMethod eim = (EmesqInspectionMethod) event.getTargetInstance();
		// now also add the dot to the title
		//设置比例，则清空数值
		//设置数值，则清空比例
		if ("CREATE".equals(eim.getType())) {
			final Property rateProperty = eimEntity.getProperty(EmesqInspectionMethod.PROPERTY_QTY);
			event.setCurrentState(rateProperty, null);
		} else {
			final Property numProperty = eimEntity.getProperty(EmesqInspectionMethod.PROPERTY_RATE);
			event.setCurrentState(numProperty, null);
		}
	}
}
